<template>
  <div class="app-container customer-div">


    <div style="margin-top:40px">
      <el-form :inline="true" :model="listQuery1" size="mini" label-width="100px">

          <el-form-item label="日期：">
            <el-date-picker
              v-model="listQuery1.date"
              type="daterange"
              format="yyyy 年 MM 月 dd 日"
              value-format="yyyy-MM-dd"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期">
            </el-date-picker>
          </el-form-item>
        <el-form-item label="品类：">
          <el-select size="mini" placeholder="请选择品类" clearable style="margin-right: 10px;width:150px"  v-model="listQuery1.goodsClass" >
            <el-option v-for="item in classList" :key="item.classId" :value="item.className" :label="item.className"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="厂家：">
          <el-input size="mini"  placeholder="请输入厂家" clearable style="width:150px;margin-right: 10px"  v-model="listQuery1.brandName"></el-input>
        </el-form-item>
        <el-form-item label="牌号：">
          <el-select v-model="listQuery1.kingGoodsId" filterable placeholder="请选择" clearable>
            <el-option
              v-for="item in goodsList"
              :key="item.kingId"
              :label="item.goodsSn"
              :value="item.kingId">
            </el-option>
          </el-select>
        </el-form-item>
          <el-form-item label="人员：">
            <el-cascader
              size="mini"
              collapse-tags
              :show-all-levels="false"
              v-model="listQuery1.userIds"
              placeholder="请选择部门/事业部/人员"
              :options="userList"
              :props="props"></el-cascader>
          </el-form-item>
        <el-form-item>
          <el-button
            @click="handleSearchList1()"
            type="primary">
            查询
          </el-button>
<!--          <el-button-->
<!--            @click="showImport2()">-->
<!--            导入-->
<!--          </el-button>-->
        </el-form-item>

      </el-form>
    </div>

    <!--数据列表-->
    <div class="table-container">
      <div style="font-size:14px;line-height: 30px;padding:10px 0 ">
        销量合计：<span style="font-weight: bold">{{goodsNum}}</span>
        <span style="padding-left:20px;">出库量合计：</span><span style="font-weight: bold">{{shipNum}}</span>
        <span style="padding-left:20px;">出库毛利合计：</span><span style="font-weight: bold">{{shipProfit}}元</span>
        <span style="padding-left:20px;">平均吨毛利：</span><span style="font-weight: bold">{{perShipProfit}}元</span>
      </div>

      <el-table
        v-loading="listLoading1"
        :data="tableData1"
        max-height="460"
        border
        style="width: 100%; margin-top: 20px">
        <el-table-column  label="客户名称" min-width="200" align="center">
          <template slot-scope="scope">
            <div>{{scope.row.customerNumber}}</div>
            <div v-if="scope.row.isShowCustomerName" style="white-space: normal;">{{scope.row.customerName}}</div>
          </template>
        </el-table-column>
        <el-table-column
          label="销售员"
          min-width="100"
          align="center">
          <template slot-scope="scope">
            {{scope.row.userRealName}}
          </template>
        </el-table-column>
        <el-table-column
          label="等级"
          min-width="100"
          align="center">
          <template slot-scope="scope">
            {{scope.row.newLevel}}
          </template>
        </el-table-column>
        <el-table-column
          label="性质"
          min-width="100"
          align="center">
          <template slot-scope="scope">
            {{scope.row.newNature}}
          </template>
        </el-table-column>
        <el-table-column
          label="新老客户"
          min-width="100"
          align="center">
          <template slot-scope="scope">
            {{scope.row.newCustomer}}
          </template>
        </el-table-column>
        <el-table-column
          label="复购时间段"
          min-width="100"
          align="center">
          <template slot-scope="scope">
            {{scope.row.repeatCustomer}}
          </template>
        </el-table-column>
        <el-table-column
          label="销量"
          min-width="100"
          align="center">
          <template slot-scope="scope">
            {{scope.row.goodsNum}}
          </template>
        </el-table-column>
        <el-table-column
          label="出库量"
          min-width="100"
          align="center">
          <template slot-scope="scope">
            {{scope.row.shipNum}}
          </template>
        </el-table-column>
        <el-table-column
          label="吨毛利"
          min-width="100"
          align="center">
          <template slot-scope="scope">
            {{scope.row.perShipProfit}}
          </template>
        </el-table-column>
        <el-table-column
          label="总毛利"
          min-width="100"
          align="center">
          <template slot-scope="scope">
            {{scope.row.shipProfit}}
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange1"
        @current-change="handleCurrentChange1"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery1.pageNum"
        :page-size="listQuery1.pageSize"
        :page-sizes="[10,15,20]"
        :total="count1">
      </el-pagination>
    </div>




  </div>
</template>
<script>
import {saleUserList,customerSaleNumAnalysis,goodsCategoryList} from '@/api/data.js'
import { orderGoodsList} from '@/api/partner'
import store from '@/store'
import { mapGetters } from 'vuex'
import { formatDate } from '@/utils/date.js';
import XLSX from "xlsx";

const defaultListQuery={
  "date" : [],
  "userIds" : [],
  "goodsClass" : "", //品类
  "brandName" : "",   //厂家
  "kingGoodsId" : "",   //指定牌号产品ID
  "pageNum" : 1,
  "pageSize" : 10
};



export default {
  name: "index",
  data(){
    return {
      roles:store.getters.roles ? store.getters.roles : [],
      props:{multiple: true,label:'value',value:'label',emitPath:false},
      listQuery1: JSON.parse(JSON.stringify(defaultListQuery)),
      tableData1: [],
      count1: 0,
      listLoading1: false,
      data:[],
      userList:[],
      classList:[],
      goodsNum:0,
      shipNum:0,
      shipProfit:0,
      perShipProfit:0,
      goodsList:[],



    }
  },
  computed: {
    ...mapGetters([
      'name',
      'mobile',
      'userId'
    ]),
  },
  created() {
    this.getUserList()
    this.getClassList()
    this.getgoodsList()
    this.listQuery1.date = this.getMonth()
    if(this.$route.query.dataDate){
      this.listQuery1.date = this.$route.query.dataDate
    }
    if(this.$route.query.dataGoodsClass){
      this.listQuery1.goodsClass = this.$route.query.dataGoodsClass
    }
    if(this.$route.query.dataBrandName){
      this.listQuery1.brandName = this.$route.query.dataBrandName
    }
    if(this.$route.query.dataUserIds){
      this.listQuery1.userIds = this.$route.query.dataUserIds.split(",")
    }
    this.getList1();
  },
  filters: {


  },
  methods:{
    getgoodsList(){
      orderGoodsList().then((res)=>{
        this.goodsList = res.list
      })
    },
    getClassList(){
      goodsCategoryList().then((res)=>{
        this.classList = res
      })
    },
    //获取本月时间
    getMonth(){
      let now = new Date();
      // 获取当前月份和年份
      const month = now.getMonth();
      const year = now.getFullYear();
      // 计算本月第一天的日期
      const startOfMonth = new Date(year, month, 1);
      // 计算下个月第一天的日期，然后减去一天得到本月的最后一天
      const startOfNextMonth = new Date(year, month + 1, 1);
      const endOfMonth = new Date(startOfNextMonth);
      endOfMonth.setDate(startOfNextMonth.getDate() - 1);
      return [formatDate(new Date(startOfMonth), 'yyyy-MM-dd'),formatDate(new Date(endOfMonth), 'yyyy-MM-dd')]
    },
    getUserList(){
      saleUserList().then((res)=>{
        this.userList = res
      })
    },
    handleSelect(key){
      this.listQuery1.tab = key
      this.handleSearchList1()
    },


    //获取列表
    getList1(){
      this.listLoading1 = true;
      let listQuery = JSON.parse(JSON.stringify(this.listQuery1));
      listQuery.userIds=listQuery.userIds.join(",")
      customerSaleNumAnalysis(listQuery).then(response => {
        this.tableData1 = response.list;
        this.count1 = response.count;
        this.listLoading1 = false;
        this.goodsNum = response.goodsNum;
        this.shipNum = response.shipNum;
        this.shipProfit = response.shipProfit;
        this.perShipProfit = response.perShipProfit;
      }).catch(()=>{
        this.listLoading1 = false;
      });
    },

    handleSizeChange1(val){
      this.listQuery1.pageNum = 1;
      this.listQuery1.pageSize = val;
      this.getList1();
    },
    handleCurrentChange1(val){
      this.listQuery1.pageNum = val;
      this.getList1();
    },

    handleSearchList1(){
      this.listQuery1.pageNum = 1;
      this.getList1();
    },


  },
}
</script>



